package com.xizi.locksupport;

public class WaitNotifyDemo {

    static Object lock = new Object();

    // 1. wait和notify方法必须要在同步块或者方法里面且成对出现使用，否则会抛出java.lang.IllegalMonitorStateException。
    // 2. 调用顺序要先wait后notify才OK。
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " come in.");
                try {
                    lock.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "被唤醒.");
        }, "Thread A").start();

        new Thread(() -> {
            synchronized (lock) {
                lock.notifyAll();
                System.out.println(Thread.currentThread().getName() + " 通知.");
            }
        }, "Thread B").start();
    }
}
